// JavaScript Document

	var tr_map;
	var geocoder;
	var tr_directionsService = new google.maps.DirectionsService();
	var from;
	var to;
	var i3 = -1;
	var flag =0;
		  
	<!--var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';-->
	<!--var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';-->
  
	function transit_calcRoute(mapornomap) 
	{
		var busd = new Array();
		var busname = new Array();
		var metrod = new Array();
		var bfare = new Array();
		var mfare = new Array();
		var metronames = new Array();
		var start = 0;
		var end = 1;						
		var j =0;
		var p =0;	
		var btot = 0;
		var mtot = 0;
		var tot;
		var str = " ";
		var metrodsum = 0;
		var bfound = 0;
		var mfound = 0;
		//console.log("Aa raha hai");
		directionsDisplay = new google.maps.DirectionsRenderer();
		var opts = 
		{
		 //center: new google.maps.LatLng(28.631257700000000000, 77.223091299999960000),
		 zoom: 12,
		 mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		tr_map = new google.maps.Map(document.getElementById('transit_map'), opts);
		google.maps.event.trigger(tr_map,'resize');
			
		//directionsDisplay.setPanel(document.getElementById('directions-panel'));
		 
		var trafficLayer = new google.maps.TrafficLayer();
        trafficLayer.setMap(tr_map);

		geocoder = new google.maps.Geocoder();
	  		
		var origin1 = document.getElementById("from").value;
		var destinationA = document.getElementById("to").value;
		var distanceInput_main = document.getElementById("transitmain_outputdist");
		var distanceInput_map = document.getElementById("transitmap_outputdist");
		$('#transitmain_outputdist').empty();
		$('#transitmap_outputdist').empty();
		var durationInput_main = document.getElementById('transitmain_outputtime');
		var durationInput_map = document.getElementById('transitmap_outputtime');
		$('#transitmain_outputtime').empty();
		$('#transitmap_outputtime').empty();
		
		/*geocoder.getLatLng(address, function(point) {
         var latitude = point.y;
         var longitude = point.x;  

         // do something with the lat lng
   		 });*/
			
		directionsDisplay.setMap(tr_map);
        //var selectedMode = document.getElementById('mode').value;
		//var depttime = localStorage.getItem('ts')/1000;
		var depttime = localStorage.getItem('ts');
		console.log("Dept time is: " + depttime);
        var request =
		{
			origin: origin1,
			destination: destinationA,
			travelMode: google.maps.TravelMode.TRANSIT,			
			/*transitOptions: 
			{
    			departureTime: depttime
 			},*/
			provideRouteAlternatives: true,
			durationInTraffic: true
        };
		
		tr_directionsService.route(request, function(response, status) 
		{
	      	if (status == google.maps.DirectionsStatus.OK) 
			{	  
				/*if(nextorprev==1)
				{	i3++;
					console.log("I pressed next! i3 changes to " + i3);	
					
				}
				else if(nextorprev==-1)
				{
					i3--;
					console.log("I pressed prev! i3 changes to " + i3);	
					
				}
					
				
		 
				if(response.routes.length < i3+1)
				{	
					console.log(i3+ " i3 has crossed the number of routes, set i3 to 0");
					i3=0;			
				}
				if(i3==-1)
				{
					i3 = response.routes.length - 1;
					console.log("Prevbutton pressed! Go to last route!, set i3 to " + i3);
				}
				if(i3<=-2)
				{
					i3=-1;
					console.log("i3 was -2, made -1");
					//console.log("Levelof stupidity: Above 9000");
				}*/
         		document.getElementById('transitmain_outputdist').innerHTML += 
            	response.routes[0].legs[0].distance.text;
         		document.getElementById('transitmap_outputdist').innerHTML += 
            	response.routes[0].legs[0].distance.text;

        		// Display the duration:
         		document.getElementById('transitmain_outputtime').innerHTML += 
            	response.routes[0].legs[0].duration.text;
         		document.getElementById('transitmap_outputtime').innerHTML += 
            	response.routes[0].legs[0].duration.text;
			
				//For duration in traffic need to buy Business maps API
				
				//distanceInput.value = response.routes[i].legs[0].distance.text;
				//durationInput.value = response.routes[i].legs[0].duration.text;
				
				//localStorage.setItem('dist', distanceInput.value);
				//localStorage.setItem('time', durationInput.value);

				//directionsDisplay.setRouteIndex(1);
                //console.log(directionsDisplay.getRouteIndex());
                //var temp = directionsDisplay.getDirections();
				if(mapornomap==1)
				{
					directionsDisplay.setDirections(response);
					google.maps.event.trigger(tr_map,'resize');
				}
				
				for(l=0;l<response.routes[0].legs[0].steps.length;l++)
				{
					if(response.routes[0].legs[0].steps[l].travel_mode == "TRANSIT")
					{
							if(response.routes[0].legs[0].steps[l].transit.line.vehicle.name == "Bus")
							{
								bfound = 1;
								//console.log("Bus found");
								busname[j] = response.routes[0].legs[0].steps[l].transit.line.short_name;
								busd[j] = response.routes[0].legs[0].steps[l].distance.text;
								if (parseFloat(busd[j]) <= 4)
									bfare[j] = 5;
								else if (parseFloat(busd[j]) > 4 && parseFloat(busd[j]) <= 10)
									bfare[j] = 10;
								else if (parseFloat(busd[j]) > 10)
									bfare[j] = 15;
								console.log("Bus " + busname[j] + " distance is " + busd[j]);
								
								console.log("Fare for bus " + busname[j] + " is " + bfare[j]); 
								str = str + " Fare for bus " + busname[j] + " is " + bfare[j] + "\n";
								j++;
							}
							else if(response.routes[0].legs[0].steps[l].transit.line.vehicle.name == "Subway")
							{
								mfound = 1;
								//console.log("Subway found");
								//console.log(response.routes[0].legs[0].steps[l].distance.text);
								metronames[start] = response.routes[0].legs[0].steps[l].transit.departure_stop.name;
								start = start + 2;
								metronames[end] = response.routes[0].legs[0].steps[l].transit.arrival_stop.name;
								end = end +2;
								metrod[p] = response.routes[0].legs[0].steps[l].distance.text;
								console.log("Metro distance: " + metrod[p]);
								metrodsum = metrodsum + parseFloat(metrod[p]);
								//console.log ("Metro sum is: " + metrodsum);
								p++;						
							}
					}				
				}
				if (mfound == 1)
				{
					if (parseFloat(metrodsum) <= 1)
						mfare = 8;
					else if (parseFloat(metrodsum) > 1)
						mfare = 8 + (parseInt(metrodsum)-1);
					if (mfare>30)
						mfare = 30;
					console.log("Fare from " + metronames[0] + " to " + metronames[metronames.length-1] + " is " + mfare);
					str = str + " Fare from " + metronames[0] + " to " + metronames[metronames.length-1] + " is " + mfare + "\n";
				}
				for (var f1=0; f1<bfare.length; f1++)
				{
					btot = btot + bfare[f1];
				}
				//console.log("Total bus fare: " + btot);	
				//console.log("Total Metro fare: " + mfare);
				tot = btot+mfare;	
				document.getElementById('transitmain_fare').innerHTML = tot;
				document.getElementById('transitmap_fare').innerHTML = str;	
				//console.log(str);
				//console.log(directionsDisplay.getRouteIndex());				
			}
      //directionsDisplay.setDirections(response);
	  });
	  			
	
	}	  
	//google.maps.event.addDomListener(window, 'load', initialize);
	
	function transit_getdirections()
	{
		directionsDisplay.setPanel(document.getElementById('transit_directions-panel'));
	}